function [val,b]=AlphaBetaPrunningCuttOff(board,depth,alpha, beta)

PrincipalVariation=0;
currentPlayer=1;
if(depth==0)% if depth=0 return value
    val=evalfunc( board, currentPlayer);
    b=board;
    fprintf('Current Player: %d\n',currentPlayer)
    fprintf('DEPTH: %d\nVALUE: %d \n\n' ,depth,val);
    return
% else
%     val=NaN;
%     b=board;
end

% if (64-size(find(board),1)==0)% if player can't play
%     val=evalfunc( board, currentPlayer);
%     b=board;
%     return
% end


% Get the list of plausable moves, sorted by most pieces flipped
moves = GetValidFields( board , currentPlayer );% weighted matrix!
[row,column,value]=find(moves);
PosMat=sortrows([row,column,value],-3);% Sortieren!
row=PosMat(:,1);
column=PosMat(:,2);
% value=PosMat(:,3);
numPosMoves = length(row);

if numPosMoves == 0   % no posibble moves
    if nargout == 2     
        b = board;      
        val = 0;          
        return;
    end
    % Gegner am Zug -> Werte negieren; alpha und beta vertauschen
%     val = -AlphaBetaPrunningCuttOff(-board,depth-1, -beta, -alpha);
    val = -NegaMax(-board, depth-1, -beta, -alpha);
    return;
end



% copy the board
% currentBoard2=board;
%  Find the best currentMove
bestMove=zeros(2,1);
CurrentMoveIdx=1;
% for CurrentMoveIdx=1:length(value)
% b=beta;
while(CurrentMoveIdx<=numPosMoves)  
% for CurrentMoveIdx=1:numPosMoves    
          fprintf('TIEFE: %d Current Index: %d\n',depth,CurrentMoveIdx)
         
         currentBoard=make_move(board,row(CurrentMoveIdx),column(CurrentMoveIdx),1);
         
        if(PrincipalVariation)
            currentVal= -AlphaBetaPrunningCuttOff(-currentBoard,depth-1, -alpha-1, -alpha);
%             currentVal= -NegaMax(-currentBoard, depth-1, -alpha-1, -alpha);  
            if ((currentVal > alpha) && (currentVal < beta))
                currentVal= -AlphaBetaPrunningCuttOff(-currentBoard,depth-1, -beta, -alpha);
%                 currentVal= -NegaMax(-currentBoard, depth-1, -beta, -alpha); 
            end                  
        else
         currentVal= -AlphaBetaPrunningCuttOff(-currentBoard,depth-1, -beta, -alpha);
%             currentVal= -NegaMax(-currentBoard, depth-1, -beta, -alpha); 
        end

        if (currentVal> alpha)        
            alpha = currentVal;
            PrincipalVariation = 1;
            bestMove = [row(CurrentMoveIdx); column(CurrentMoveIdx)];
            CurrentMoveIdx=CurrentMoveIdx-1;
            if (currentVal>= beta)    
%                 val=currentVal;
                val=beta;
                return;          
            end
        end
        CurrentMoveIdx=CurrentMoveIdx+1;
end
% end

val=alpha;
if nargout == 2 
    display(bestMove)
%     if ((bestMove(1)==0)&&(bestMove(2)==0))
%         b=board;
%     else
        b=make_move(board,bestMove(1),bestMove(2),1);
%     end
    display(b)
end


display(val)




